﻿using System.Windows;
using System.Windows.Controls;
using compshop.Modules.PositionList.Presenter;
using compshop.Infrastructure.CompShopDataService;


namespace compshop.Modules.PositionList.View
{
    /// <summary>
    /// Логика взаимодействия для ClientOrderListView.xaml
    /// </summary>
    public partial class PositionListView : UserControl, IViewPositionList
    {
        public PositionListPresenter Presenter { get; set; }

        public PositionListView()
        {
            InitializeComponent();
        }

        public PositionListView(PositionListPresenter prsnt)
            : this()
        {
            Presenter = prsnt;
            Presenter.View = this;
            DataContext = prsnt;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (lvPositionList.SelectedValue != null)
            Presenter.ShowPositionDialog(lvPositionList.SelectedValue as Position);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ShowPositionDialog(null);
        }

        private void lvPositionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Position selected=(sender as ListView).SelectedValue as Position;
            if (selected != null)
                Presenter.onSelectedPosition((int)selected.PositionID);
        }

        public void ShowData()
        {
            lvPositionList.UpdateLayout();
        }

        public void UpdateData()
        {
            lvPositionList.ItemsSource = null;
            lvPositionList.ItemsSource = Presenter.Positions;
            lvPositionList.UpdateLayout();
        }
    }
}
